<?php

namespace App\Http\Models\User;

use App\Common\Utils\Common;
use App\Http\Models\User\User;
use App\Http\Models\Role\Role;
use Illuminate\Support\Facades\DB;
use App\Http\Models\Log\LogService;
use Illuminate\Support\Facades\Cache;
use App\Http\Service\menus\MenusUserService;

/*
 * UserService  用户操作服务只处理逻辑
 *
 * index() 用户列表
 * show() 用户查看
 * store() 新增用户
 * updateSave() 用户修改
 * remove() 删除用户
 * getRole() 获取角色
 *
 * @author 金飞虎
 * @email 870741306@qq.com
 * @date 2018/4/5
 * */

class UserService extends User
{

    public function index($request)
    {
        $where = [
            'delete'       => 1,
            'users_status' => 0
        ];
        return User::select()
            ->where($where)
            ->where('name', 'like', '%' . $request->name . '%')
            ->orderBy('created_at', 'desc')
            ->paginate(PAGES);
    }

    public function store($request)
    {
        $data = [
            'name'           => $request->name,
            'username'       => $request->username,
            'password'       => bcrypt($request->password),
            'phone'          => $request->phone,
            'status'         => $request->status,
            'level'          => $request->level,
            'images'         => $request->images,
            'synopsis'       => $request->synopsis ?? "",
            'users_status'   => 0,
            'remember_token' => bcrypt($request->password),
        ];
        //开启事物
        DB::beginTransaction();
        try {
            $res = User::create($data);
            if ($res->id) {
                Common::success(INSERT_SUCCES);
                //LogService::log()->create(INSERT_USER . "-" . $request->username);
                DB::commit();
                return $res;
            } else {
                Common::success(INSERT_ERROR);
            }
        } catch (Exception $ex) {
            DB::rollBack();
            Common::error(INSERT_ERROR);
        }
    }

    public function updateSave($request, $id)
    {
        //插入菜单
        $data = [
            'name'     => $request->name,
            'username' => $request->username,
            'password' => bcrypt($request->password),
            'phone'    => $request->phone,
            'status'   => $request->status,
            'images'   => $request->images,
            'level'    => $request->level,
            'synopsis' => $request->synopsis ? $request->synopsis : ""
        ];
        if (empty($request->password)) {
            unset($data['password']);
        }
        try {
            $res = User::where('id', decryptUrl($id))->update($data);
            if ($res) {
                Common::success(UPDATE_SUCCES);
                return $res;
            } else {
                Common::error(UPDATE_ERROR);
            }
        } catch (Exception $ex) {
            Common::error(UPDATE_ERROR);
        }
    }

    public function show($id)
    {
        $userData = User::where('id', decryptUrl($id))->first();
        $userData->tmpImages = $userData->images;
        $userData->images = $userData->images;
        return $userData;
    }

    public function getRoleAll()
    {
        return Role::orderBy('created_at', 'desc')->get();
    }

    public function remove($id)
    {
        $data = [
            'delete' => 0,
        ];
        Cache::put(md5(LEFT_DATAS_KEY . decryptUrl($id)), '');
        LogService::log()->create(DELETE_USER);
        return User::where('id', decryptUrl($id))->update($data);
    }

    //获取私教
    public function getPrivateEducation()
    {
        return User::where(['users_status' => 0, 'status' => 1])->get()->toArray();
    }
}
